let express = require("express");
let path = require("path");

let app = express();
//处理body请求体的中间件
app.use(express.urlencoded());
app.use(express.json());

//查询信息的接口
app.get("/user", (request, response) => {
  let token = request.headers.token;
  console.log(token, "token");
  if (!token) {
    return response.send({
      code: 10002,
      msg: "无对应权限",
      data: null,
    });
  }

  let {userid} = request.query;

  //查询失败的情况
  if (userid !== "001") {
    return response.send({
      code: 10001,
      msg: "查询失败",
      data: null,
    });
  }

  //查询成功 响应
  response.send({
    code: 10000,
    msg: "查询成功",
    data: {
      name: "李博",
      age: 18,
      sex: "男",
    },
  });

});

//定义一个延迟响应的请求接口
app.get('/userdelay',(request, response) => {
    setTimeout(()=>{
        let token = request.headers.token;
  console.log(token, "token");
  if (!token) {
    return response.send({
      code: 10002,
      msg: "无对应权限",
      data: null,
    });
  }

  let {userid} = request.query;

  //查询失败的情况
  if (userid !== "001") {
    return response.send({
      code: 10001,
      msg: "查询失败",
      data: null,
    });
  }

  //查询成功 响应
  response.send({
    code: 10000,
    msg: "查询成功",
    data: {
      name: "李博",
      age: 18,
      sex: "男",
    },
  });
    },2000)
})

//静态资源部署接口

app.get('/1',(request, response)=>{
    let filePath = path.resolve(__dirname,'./创建axios实例.html')
    response.sendFile(filePath)
})

app.get('/2',(request, response)=>{
    let filePath = path.resolve(__dirname,'./请求拦截器.html')
    response.sendFile(filePath)
})

app.get('/3',(request, response)=>{
    let filePath = path.resolve(__dirname,'./响应拦截器.html')
    response.sendFile(filePath)
})

app.get('/4',(request, response)=>{
    let filePath = path.resolve(__dirname,'./拦截器练习.html')
    response.sendFile(filePath)
})

app.get('/5',(request, response)=>{
    let filePath = path.resolve(__dirname,'./取消请求.html')
    response.sendFile(filePath)
})

app.listen(1010,()=>{
    console.log('running');
})